<template>
  <div class="radioSelect" :style="{ cursor: disabled ? 'not-allowed' : 'pointer' }">
    <div style="display: flex; align-items: center; height: 100%">
      <el-popover v-model:visible="visible" placement="bottom-start" width="150" trigger="manual" popper-class="message-popover">
        <template #reference>
          <div v-show="!activeName" class="icon" @click="disabled ? (visible = false) : (visible = true)"></div>
          <el-avatar v-show="activeName" style="background: rgb(0, 196, 186)">
            {{ activeName ? activeName.slice(0, 1) : activeName }}
          </el-avatar>
        </template>

        <ul v-if="options.length > 0" class="group">
          <li v-for="(item, index) in dataList" :key="index" class="radio" @click="select(item)">
            {{ item.label }}
          </li>
        </ul>
        <div v-else class="empty">无数据</div>
      </el-popover>

      <span class="title">
        <div>{{ activeName || '--' }}</div>
        <div><slot></slot></div>
      </span>

      <div class="del">
        <i v-if="!disabled" class="el-icon-close" @click="clear"></i>
      </div>
    </div>
  </div>
</template>

<script setup name="RadioSelect" lang="ts">
import { ref, computed, watch, onMounted } from 'vue';

defineProps({
  value: [Number, String],
  options: {
    type: Array,
    default: () => []
  },
  disabled: {
    type: Boolean,
    default: false
  },
  keyName: String,
  valueName: String
});

const emit = defineEmits(['update:value', 'change']);

const visible = ref(false);
const activeIndex = ref(null);
const activeName = ref(null);

// 处理选项数据列表
const dataList = computed(() => {
  if (props.options.length > 0) {
    return props.options.map((item) => ({
      label: item[props.keyName || 'label'],
      value: item[props.valueName || 'value']
    }));
  }
  return [];
});

// 选择操作
const select = (data) => {
  activeIndex.value = data.value;
  activeName.value = data.label;
  emit('update:value', data.value);
  visible.value = false;
  emit('change', data.value);
};

// 清空操作
const clear = () => {
  select({ value: null, label: '' });
};

// 监听 value 变化并初始化状态
watch(
  () => props.value,
  (newVal) => {
    const target = dataList.value.find((item) => item.value === newVal);
    if (target) select(target);
  },
  { immediate: true }
);

// 点击外部关闭 popover
onMounted(() => {
  document.addEventListener('mouseup', (e) => {
    const dom1 = document.querySelector('.radioSelect');
    if (dom1 && !dom1.contains(e.target)) {
      visible.value = false;
    }
  });
});
</script>

<style>
.message-popover {
  padding: 0 !important;
}
</style>

<style lang="scss" scoped>
.radioSelect {
  cursor: pointer;
  user-select: none;
  margin-right: 10px;
  height: 50px;
  display: flex;
  align-items: center;
  padding-left: 10px;

  .icon {
    margin-right: 10px;
    width: 35px;
    height: 35px;
    border: 1px solid #aaa;
    box-sizing: border-box;
    color: #aaa;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;

    &:hover {
      border-color: rgb(29, 167, 106);

      &::before,
      &::after {
        background: rgb(29, 167, 106);
      }
    }

    &::before,
    &::after {
      display: block;
      content: '';
      position: absolute;
      background: #aaa;
      transform: translate(-50%, -50%);
    }

    &::before {
      width: 30%;
      height: 2px;
      left: 50%;
      top: 50%;
    }

    &::after {
      height: 30%;
      width: 2px;
      left: 50%;
      top: 50%;
    }
  }

  .el-avatar {
    width: 35px;
    height: 35px;
    line-height: 35px;
    margin-right: 10px;
    font-size: 15px;
    overflow: inherit;
  }

  .title {
    height: 35px;
    color: #aaa;
    width: 70px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;

    div {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;

      &:first-of-type {
        color: #666;
        font-size: 16px;
      }

      &:last-of-type {
        color: #999;
        font-size: 12px;
      }
    }
  }

  .del {
    width: 20px;
    display: flex;
    align-items: center;

    .el-icon-close {
      visibility: hidden;
    }
  }

  &:hover .el-icon-close {
    visibility: visible;

    &:hover {
      color: red;
    }
  }
}

.group ::v-deep {
  list-style: none;
  padding: 0;
  margin: 0;
  max-height: 200px;
  overflow: auto;

  .radio {
    cursor: pointer;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    height: 35px;
    line-height: 35px;
    padding: 0 10px;

    &:hover {
      background: #eee;
    }
  }
}

.empty {
  text-align: center;
}
</style>
